package com.xneure.database;

import org.springframework.jdbc.core.JdbcTemplate;
import com.xneure.database.sql.SqlMapping;
import com.xneure.database.sql.handler.HandlerType;
import com.xneure.database.sql.handler.SqlHandler;
import com.xneure.utils.SpringUtil;

public class DataBaseExecutor{
	private JdbcTemplate template;

	public DataBaseExecutor(){
		this(SpringUtil.getJdbcTemplate());
	}

	public DataBaseExecutor(JdbcTemplate template){
		this.template = template;
	}

	public JdbcTemplate getTemplate(){
		return template;
	}

	public void setTemplate(JdbcTemplate template){
		this.template = template;
	}
	public int insert(Object obj) {
		SqlHandler _db = SqlMapping.getHandler(HandlerType.INSERT,obj.getClass());
		_db.build(obj);
		return template.update(_db.getSql());
	}
	public int updata(Object obj) {
		SqlHandler _db = SqlMapping.getHandler(HandlerType.UPDATE,obj.getClass());
		_db.build(obj);
		return template.update(_db.getSql());
	}
	public int delete(Object obj) {
		SqlHandler _db = SqlMapping.getHandler(HandlerType.DELETE,obj.getClass());
		_db.build(obj);
		return template.update(_db.getSql());
	}
}
